import 'package:flutter/material.dart';

import '../../../../helper/utils/util.dart';
import '../../main/widgets/code_field.dart';
import '../../main/widgets/filed.dart';
import '../../main/widgets/read.dart';
import 'index.dart';

class RegisterPage extends GetView<RegisterController> {
  const RegisterPage({Key? key}) : super(key: key);

  // 主视图
  Widget _buildView() {
    var state = controller.state;
    return ListView(
      shrinkWrap: true,
      children: [
        UITextField(
          margin: EdgeInsets.symmetric(horizontal: 30.r),
          title: '账号',
          hintText: '请输入手机号',
          data: state.account,
          autoFocus: true,

          titleStyle: TextStyle(
            color: const Color(0xff333333),
            fontSize: 20.sp,
            fontWeight: FontWeight.bold,
          ),
          keyboardType: TextInputType.phone,
          onChanged: (p0) {
            state.account = p0;
            if (state.accountError != null) {
              state.accountError = null;
              controller.update();
            }
          },
          style: TextFieldStyle.underLine,
          padding: EdgeInsets.zero,
          height: 50.r,
          // backgroundColor: Colors.amber,
        ),
        SizedBox(height: 10.r),
        Container(
          alignment: Alignment.centerLeft,
          margin: EdgeInsets.symmetric(horizontal: 30.r),
          // decoration: const BoxDecoration(
          //     border: Border(
          //         bottom:
          //             BorderSide(color: Color(0xffDFDFDF), width: 1))),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Container(
                alignment: Alignment.centerLeft,
                child: Text(
                  '验证码',
                  style: TextStyle(
                    color: const Color(0xff333333),
                    fontSize: 20.sp,
                    fontWeight: FontWeight.bold,
                  ),
                ),
              ),
              SizedBox(
                height: 50.r,
                child: ICodeField(
                  smsClickListener: () async {
                    return await controller.sendCode();
                  },
                  // isLineStyle: ,
                  errText: state.pwdError,
                  onChange: (p0) {
                    state.smCode = p0;
                    state.pwdError = null;
                    controller.update();
                  },
                ),
              ),
            ],
          ),
        ),
        SizedBox(height: 10.r),
        LoginField(
          controller: state.pwdCtrl,
          title: '密码',
          placeholder: '请输入密码',
          padding: EdgeInsets.symmetric(horizontal: 30.r),
          isPwd: true,
          errText: state.pwdError,
          titleColor: ColorsUtil.title,
          onChange: (p0) {
            if (state.pwdError != null) {
              state.pwdError = null;
              controller.update();
            }
          },
        ),
        SizedBox(height: 10.r),
        LoginField(
          controller: state.pwd2Ctrl,
          title: '确认密码',
          placeholder: '请输入确认密码',
          padding: EdgeInsets.symmetric(horizontal: 30.r),
          isPwd: true,
          errText: state.pwdError,
          titleColor: ColorsUtil.title,
          onChange: (p0) {
            if (state.pwdError != null) {
              state.pwdError = null;
              controller.update();
            }
          },
        ),
        SizedBox(height: 10.r),
        InkWell(
          onTap: () {
            if (!state.isReaded) {
              var p0 = state.readAnim!;
              if (p0.isAnimating) {
                return;
              }
              p0.repeat();
              Future.delayed(const Duration(milliseconds: 600)).then((value) {
                p0.stop();
              });
            } else {
              controller.actionRegister();
            }
          },
          child: Container(
            margin: EdgeInsets.symmetric(horizontal: 30.r),
            height: 50.r,
            alignment: Alignment.center,
            decoration: BoxDecoration(
              color: ColorsUtil.blue,
              borderRadius: BorderRadius.circular(5.r),
            ),
            child: Text(
              '注册',
              style: TextStyle(
                color: Colors.white,
                fontSize: 17.sp,
              ),
            ),
          ),
        ),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<RegisterController>(
      builder: (_) {
        return Scaffold(
          appBar: detailAppBar(context, title: '注册'),
          backgroundColor: Colors.white,
          body: Column(
            children: [
              Expanded(child: _buildView()),
              SafeArea(
                child: ReadDealWidget(
                  actionAnimate: (p0) {
                    controller.state.readAnim = p0;
                  },
                  valueChanged: (value) {
                    controller.state.isReaded = value;
                  },
                ),
              ),
            ],
          ),
        );
      },
    );
  }
}
